package cn.wowjoy.pda.android_pharmacy.ui.activity.door.merge;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.common.core.BaseActivity;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.NetWorkUtils;
import cn.wowjoy.pda.android_pharmacy.common.utils.SimpleItemDecoration;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.databinding.ActivityDoorMergeBinding;
import cn.wowjoy.pda.android_pharmacy.model.commonEntity.DoorMergeEntity;
import gear.yc.com.gearlibrary.rxjava.rxbus.RxBus;
import io.reactivex.functions.Consumer;

public class DoorMergeActivity extends BaseActivity<ActivityDoorMergeBinding,DoorMergeViewModel,DoorMergePresenter> {

    //PDA扫码广播
    private ScanReceiver mScanReceiver;
    private IntentFilter intentFilter;
    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setPresenter(presenter);
        binding.setViewModel(viewModel);
        mScanReceiver = new ScanReceiver();
        intentFilter = new IntentFilter();
        intentFilter.addAction(Contants.WOW_NEW_ADD);

        binding.doorIndexTitle.titleTextTv.setText("门诊药房");
        binding.doorIndexTitle.titleTextTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.getOrderInfo("706327");
            }
        });
        binding.doorIndexTitle.titleBackTv.setText("");
        binding.doorIndexTitle.titleBackLl.setVisibility(View.VISIBLE);
        binding.doorIndexTitle.titleBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
//        binding.recyclerview.setEmptyView(binding.emptyView.getRoot());
        binding.recyclerview.setAdapter(presenter.mDoorMergeIndexAdapter);
        //     viewModel.mAdapter.setDone(isDone);
        presenter.adapter.removeFooterView();
        presenter.adapter.removeHeaderView();
        binding.recyclerview.addItemDecoration(new SimpleItemDecoration(this, SimpleItemDecoration.VERTICAL_LIST));
//        binding.recyclerview.setPullRefreshEnabled(false);
//        binding.recyclerview.setPullRefreshEnabled(false);

        binding.btMerge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null == presenter.mDoorMergeIndexAdapter.mMergeVORequest){
                    return;
                }
                   presenter.merge(presenter.mDoorMergeIndexAdapter.mMergeVORequest);
            }
        });


    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_door_merge;
    }

    @Override
    protected Class<DoorMergePresenter> getPresenterClass() {
        return DoorMergePresenter.class;
    }

    @Override
    protected Class<DoorMergeViewModel> getViewModelClass() {
        return DoorMergeViewModel.class;
    }
    private String patientId="";
    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mScanReceiver, intentFilter);
        presenter.getToken();
        RxBus.getInstance().tObservable(Contants.DOOR_MERGE_ORDERINFO, DoorMergeEntity.class).subscribe(new Consumer<DoorMergeEntity>() {
            @Override
            public void accept(DoorMergeEntity entity) throws Exception {
                if(presenter.mDoorMergeEntities.size() == 0){

                    patientId = entity.getResults().getPrescription().getPatientId();
                    binding.setModel(entity);
                    presenter.mDoorMergeEntities.add(entity);
                    presenter.mDoorMergeIndexAdapter.addDate(entity);
                    binding.recyclerview.setVisibility(View.VISIBLE);
                    binding.btMerge.setVisibility(View.VISIBLE);
                    binding.btMerge.setText("合并处方("+presenter.mDoorMergeEntities.size()+")");
                    binding.rlDetail.setVisibility(View.VISIBLE);
                    binding.imgvSample.setVisibility(View.VISIBLE);

                    binding.rlIndex.setVisibility(View.GONE);
                }else{
                    if(patientId.equals(entity.getResults().getPrescription().getPatientId())){

                        presenter.mDoorMergeEntities.add(entity);
                        presenter.mDoorMergeIndexAdapter.addDate(entity);
                        binding.btMerge.setText("合并处方("+presenter.mDoorMergeEntities.size()+")");
                    }else{
                        new MyToast(DoorMergeActivity.this).showinfo("当前扫到的单子不是同一个病人的，无法合并请确认");
                    }
                }
            }
        });


        RxBus.getInstance().tObservable(Contants.DOOR_MERGE_EOORO, DoorMergeEntity.class).subscribe(new Consumer<String>() {
            @Override
            public void accept(String entity) throws Exception {
               new MyToast(DoorMergeActivity.this).showinfo(entity);
            }
        });
    }
    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(mScanReceiver);
    }
    //TODO:接收PDA扫码广播，准备跳转
    class ScanReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Contants.WOW_NEW_ADD)) {
                //要做下扫码的过滤判断
                String stringExtra = intent.getStringExtra(Contants.WOW_NEW_KEY);
                if(NetWorkUtils.isWifi(DoorMergeActivity.this)){
                    if(null != stringExtra && stringExtra.length()>1){
                        //TODO:
                        // 1.单子是否是乱码
                        // 2.判断当前的单子是否是同一个人的
                        presenter.getOrderInfo(stringExtra);
                    }
                }else{
                    CreateDialog.failDialog3(DoorMergeActivity.this,"错误","当前网络不可用，请检查你的网络设置");
                }
            }
        }
    }
}
